package cn.hengzhu.main_manager.web.controller;

import cn.hengzhu.main_manager.repository.dao.HelpCenterMapper;
import cn.hengzhu.main_manager.repository.domain.HelpCenter;
import cn.hengzhu.main_manager.repository.domain.HelpCenterExample;
import cn.hengzhu.main_manager.repository.param.PageBaseParam;
import cn.hengzhu.main_manager.utils.AjaxResult;
import cn.hengzhu.main_manager.utils.LayUiResultJson;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * @author 周磊
 * @create 2019-03-16 13:38
 */
@Controller
@RequestMapping("/applet")
public class AppletController {
    @Autowired
    private HelpCenterMapper helpCenterMapper;

    @RequestMapping("/add")
    public String add(){
        return "FAQAdd";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(HelpCenter helpCenter){
        if(helpCenter.getSign()!=3){
            helpCenter.setTime(new Date());
        }
        int i = helpCenterMapper.updateByPrimaryKeySelective(helpCenter);
        if(i == 1 ){
            return new AjaxResult();
        }
        return new AjaxResult("请重新添加");
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayUiResultJson<HelpCenter> list(PageBaseParam param, Integer sgin){
        HelpCenterExample example = new HelpCenterExample();
        example.createCriteria().andSignEqualTo(sgin);
        PageHelper.startPage(param.getPage(),param.getLimit(),true);
        List<HelpCenter> list = helpCenterMapper.selectByExample(example);
        PageInfo<HelpCenter> pageInfo = new PageInfo<>(list);

        long count = pageInfo.getTotal();
        LayUiResultJson<HelpCenter> result = new LayUiResultJson<>(count, list);

        return result;
    }

    @RequestMapping("/insert")
    @ResponseBody
    public AjaxResult addApplet(HelpCenter helpCenter){
        System.out.println(helpCenter.getTitle());
        helpCenter.setTime(new Date());
        int i = helpCenterMapper.insertSelective(helpCenter);
        System.out.println(i);
        if(i == 1 ){
            return new AjaxResult();
        }
        return new AjaxResult("请重新添加");
    }

}
